package com.gjp.processdemo.controller;

import lombok.extern.slf4j.Slf4j;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.service.FlowableModelQueryService;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@Slf4j
public class ProcessController {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    protected FlowableModelQueryService modelQueryService;
    @Autowired
    private ModelService modelService;

    /**
     * 查询流程定义列表
     *
     * @return
     */
    @RequestMapping("/process/list")
    public ModelAndView getProcessList() {
        ModelAndView modelAndView = new ModelAndView("process-list");
        List<ProcessDefinition> list = repositoryService
                .createProcessDefinitionQuery()
                .active()
                .latestVersion()
                .list();
        modelAndView.addObject("processList", list);
        return modelAndView;
    }

    /**
     * 查询流程定义列表
     *
     * @return
     */
    @RequestMapping("/model/list")
    public ModelAndView getModelList(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("model-list");
        ResultListDataRepresentation models = modelQueryService.getModels(null, null, null, request);
        modelAndView.addObject("modelList", models.getData());
        return modelAndView;
    }

    /**
     * 查询流程定义列表
     *
     * @return
     */
    @RequestMapping("/deploy/model/{modelId}")
    public Object deployModel(@PathVariable String modelId) {
        Model model = modelService.getModel(modelId);
        BpmnModel bpmnModel = modelService.getBpmnModel(model);
        repositoryService
                .createDeployment()
                .name(model.getName())
                .addBpmnModel(model.getKey() + ".bpmn", bpmnModel)
                .deploy();
        return "部署成功";
    }


    /**
     * 启动流程
     *
     * @param processKey
     * @return
     */
    @RequestMapping("/start/{processKey}")
    public ModelAndView startProcessByKey(@PathVariable("processKey") String processKey) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView();
        ProcessInstance processInstance;
        try {
            //启动流程
            processInstance = runtimeService.startProcessInstanceByKey(processKey);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        log.debug("processInstance：{}", processInstance.getId());
        modelAndView.setViewName("redirect:/myTask");
        return modelAndView;
    }
}
